/**
 * Copyright 2009 Henning Teek, Ingo Renner
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.googlecode.chigs.web.admin.variant;

import org.apache.wicket.authorization.strategies.role.annotations.AuthorizeInstantiation;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.image.Image;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.model.CompoundPropertyModel;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.StringResourceModel;
import org.apache.wicket.spring.injection.annot.SpringBean;

import com.googlecode.chigs.domain.Variant;
import com.googlecode.chigscommons.repository.GenericDomainService;
import com.googlecode.chigscommons.web.common.IconFactory;
import com.googlecode.chigscommons.web.common.ValidationStyleBehavior;
import com.googlecode.chigscommons.web.common.forms.ShinyForm;

/**
 * 
 * @author Ingo Renner
 * @author Henning Teek
 *
 */
@AuthorizeInstantiation("ROLE_ADMIN")
public class EditOrCreateVariantPanel extends Panel {
	@SpringBean
	private GenericDomainService genericDomainService;

	public EditOrCreateVariantPanel(String id) {
		this(id, new CompoundPropertyModel<Variant>(new Variant()));
	}

	public EditOrCreateVariantPanel(String id, IModel<Variant> model) {
		super(id);
		add(new EditOrCreateVariantForm("variant.form", model));
	}

	class EditOrCreateVariantForm extends ShinyForm<Variant> {
		public EditOrCreateVariantForm(String id, IModel<Variant> model) {
			super(id, model);
			StringResourceModel lName = new StringResourceModel("name.label", EditOrCreateVariantForm.this, null);
			add(new Label("name.label", lName));
			add(new TextField<String>("name")
					.setRequired(true)
					.setLabel(lName)
					.add(new ValidationStyleBehavior()));

			StringResourceModel lDesc = new StringResourceModel("description.label", EditOrCreateVariantForm.this, null);
			add(new Label("description.label", lDesc));
			add(new TextField<String>("description")
					.setRequired(true)
					.setLabel(lDesc)
					.add(new ValidationStyleBehavior()));

			add(new Image("submit", IconFactory.createOkIconReference()));
			Link<Void> cancelLink = new Link<Void>("cancelButton") {
				@Override
				public void onClick() {
					returnToManagement();
				}
			};
			cancelLink.add(new Image("icon", IconFactory.createCancelIconReference()));
			add(cancelLink);
		}

		@Override
		protected void onSubmit() {
			try {
				genericDomainService.save(Variant.class, getModelObject());
				getSession().info(new StringResourceModel("save.success", EditOrCreateVariantForm.this, null).getString());
				returnToManagement();
			} catch (Exception e) {
				error(e.getMessage());
			}
		}		

		private void returnToManagement() {
			final Panel p = new ManageVariantsPanel(EditOrCreateVariantPanel.this.getId());
			p.setOutputMarkupId(true);
			EditOrCreateVariantPanel.this.getParent().addOrReplace(p);
			this.setVisible(false);
		}
	}
}

